home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / destest.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  25KB  |  795 lines

  1. /* lib/des/destest.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #ifndef MSDOS
  51. #include <unistd.h>
  52. #else
  53. #include <io.h>
  54. #endif
  55. #include <string.h>
  56. #include "des.h"
  57.  
  58. /* tisk tisk - the test keys don't all have odd parity :-( */
  59. /* test data */
  60. #define NUM_TESTS 34
  61. static unsigned char key_data[NUM_TESTS][8]={
  62.     {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  63.     {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
  64.     {0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  65.     {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
  66.     {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
  67.     {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
  68.     {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  69.     {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10},
  70.     {0x7C,0xA1,0x10,0x45,0x4A,0x1A,0x6E,0x57},
  71.     {0x01,0x31,0xD9,0x61,0x9D,0xC1,0x37,0x6E},
  72.     {0x07,0xA1,0x13,0x3E,0x4A,0x0B,0x26,0x86},
  73.     {0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E},
  74.     {0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6},
  75.     {0x01,0x13,0xB9,0x70,0xFD,0x34,0xF2,0xCE},
  76.     {0x01,0x70,0xF1,0x75,0x46,0x8F,0xB5,0xE6},
  77.     {0x43,0x29,0x7F,0xAD,0x38,0xE3,0x73,0xFE},
  78.     {0x07,0xA7,0x13,0x70,0x45,0xDA,0x2A,0x16},
  79.     {0x04,0x68,0x91,0x04,0xC2,0xFD,0x3B,0x2F},
  80.     {0x37,0xD0,0x6B,0xB5,0x16,0xCB,0x75,0x46},
  81.     {0x1F,0x08,0x26,0x0D,0x1A,0xC2,0x46,0x5E},
  82.     {0x58,0x40,0x23,0x64,0x1A,0xBA,0x61,0x76},
  83.     {0x02,0x58,0x16,0x16,0x46,0x29,0xB0,0x07},
  84.     {0x49,0x79,0x3E,0xBC,0x79,0xB3,0x25,0x8F},
  85.     {0x4F,0xB0,0x5E,0x15,0x15,0xAB,0x73,0xA7},
  86.     {0x49,0xE9,0x5D,0x6D,0x4C,0xA2,0x29,0xBF},
  87.     {0x01,0x83,0x10,0xDC,0x40,0x9B,0x26,0xD6},
  88.     {0x1C,0x58,0x7F,0x1C,0x13,0x92,0x4F,0xEF},
  89.     {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
  90.     {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
  91.     {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
  92.     {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  93.     {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
  94.     {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
  95.     {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10}};
  96.  
  97. static unsigned char plain_data[NUM_TESTS][8]={
  98.     {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  99.     {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
  100.     {0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
  101.     {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
  102.     {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
  103.     {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
  104.     {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  105.     {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
  106.     {0x01,0xA1,0xD6,0xD0,0x39,0x77,0x67,0x42},
  107.     {0x5C,0xD5,0x4C,0xA8,0x3D,0xEF,0x57,0xDA},
  108.     {0x02,0x48,0xD4,0x38,0x06,0xF6,0x71,0x72},
  109.     {0x51,0x45,0x4B,0x58,0x2D,0xDF,0x44,0x0A},
  110.     {0x42,0xFD,0x44,0x30,0x59,0x57,0x7F,0xA2},
  111.     {0x05,0x9B,0x5E,0x08,0x51,0xCF,0x14,0x3A},
  112.     {0x07,0x56,0xD8,0xE0,0x77,0x47,0x61,0xD2},
  113.     {0x76,0x25,0x14,0xB8,0x29,0xBF,0x48,0x6A},
  114.     {0x3B,0xDD,0x11,0x90,0x49,0x37,0x28,0x02},
  115.     {0x26,0x95,0x5F,0x68,0x35,0xAF,0x60,0x9A},
  116.     {0x16,0x4D,0x5E,0x40,0x4F,0x27,0x52,0x32},
  117.     {0x6B,0x05,0x6E,0x18,0x75,0x9F,0x5C,0xCA},
  118.     {0x00,0x4B,0xD6,0xEF,0x09,0x17,0x60,0x62},
  119.     {0x48,0x0D,0x39,0x00,0x6E,0xE7,0x62,0xF2},
  120.     {0x43,0x75,0x40,0xC8,0x69,0x8F,0x3C,0xFA},
  121.     {0x07,0x2D,0x43,0xA0,0x77,0x07,0x52,0x92},
  122.     {0x02,0xFE,0x55,0x77,0x81,0x17,0xF1,0x2A},
  123.     {0x1D,0x9D,0x5C,0x50,0x18,0xF7,0x28,0xC2},
  124.     {0x30,0x55,0x32,0x28,0x6D,0x6F,0x29,0x5A},
  125.     {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
  126.     {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
  127.     {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
  128.     {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
  129.     {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  130.     {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  131.     {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};
  132.  
  133. static unsigned char cipher_data[NUM_TESTS][8]={
  134.     {0x8C,0xA6,0x4D,0xE9,0xC1,0xB1,0x23,0xA7},
  135.     {0x73,0x59,0xB2,0x16,0x3E,0x4E,0xDC,0x58},
  136.     {0x95,0x8E,0x6E,0x62,0x7A,0x05,0x55,0x7B},
  137.     {0xF4,0x03,0x79,0xAB,0x9E,0x0E,0xC5,0x33},
  138.     {0x17,0x66,0x8D,0xFC,0x72,0x92,0x53,0x2D},
  139.     {0x8A,0x5A,0xE1,0xF8,0x1A,0xB8,0xF2,0xDD},
  140.     {0x8C,0xA6,0x4D,0xE9,0xC1,0xB1,0x23,0xA7},
  141.     {0xED,0x39,0xD9,0x50,0xFA,0x74,0xBC,0xC4},
  142.     {0x69,0x0F,0x5B,0x0D,0x9A,0x26,0x93,0x9B},
  143.     {0x7A,0x38,0x9D,0x10,0x35,0x4B,0xD2,0x71},
  144.     {0x86,0x8E,0xBB,0x51,0xCA,0xB4,0x59,0x9A},
  145.     {0x71,0x78,0x87,0x6E,0x01,0xF1,0x9B,0x2A},
  146.     {0xAF,0x37,0xFB,0x42,0x1F,0x8C,0x40,0x95},
  147.     {0x86,0xA5,0x60,0xF1,0x0E,0xC6,0xD8,0x5B},
  148.     {0x0C,0xD3,0xDA,0x02,0x00,0x21,0xDC,0x09},
  149.     {0xEA,0x67,0x6B,0x2C,0xB7,0xDB,0x2B,0x7A},
  150.     {0xDF,0xD6,0x4A,0x81,0x5C,0xAF,0x1A,0x0F},
  151.     {0x5C,0x51,0x3C,0x9C,0x48,0x86,0xC0,0x88},
  152.     {0x0A,0x2A,0xEE,0xAE,0x3F,0xF4,0xAB,0x77},
  153.     {0xEF,0x1B,0xF0,0x3E,0x5D,0xFA,0x57,0x5A},
  154.     {0x88,0xBF,0x0D,0xB6,0xD7,0x0D,0xEE,0x56},
  155.     {0xA1,0xF9,0x91,0x55,0x41,0x02,0x0B,0x56},
  156.     {0x6F,0xBF,0x1C,0xAF,0xCF,0xFD,0x05,0x56},
  157.     {0x2F,0x22,0xE4,0x9B,0xAB,0x7C,0xA1,0xAC},
  158.     {0x5A,0x6B,0x61,0x2C,0xC2,0x6C,0xCE,0x4A},
  159.     {0x5F,0x4C,0x03,0x8E,0xD1,0x2B,0x2E,0x41},
  160.     {0x63,0xFA,0xC0,0xD0,0x34,0xD9,0xF7,0x93},
  161.     {0x61,0x7B,0x3A,0x0C,0xE8,0xF0,0x71,0x00},
  162.     {0xDB,0x95,0x86,0x05,0xF8,0xC8,0xC6,0x06},
  163.     {0xED,0xBF,0xD1,0xC6,0x6C,0x29,0xCC,0xC7},
  164.     {0x35,0x55,0x50,0xB2,0x15,0x0E,0x24,0x51},
  165.     {0xCA,0xAA,0xAF,0x4D,0xEA,0xF1,0xDB,0xAE},
  166.     {0xD5,0xD4,0x4F,0xF7,0x20,0x68,0x3D,0x0D},
  167.     {0x2A,0x2B,0xB0,0x08,0xDF,0x97,0xC2,0xF2}};
  168.  
  169. static unsigned char cipher_ecb2[NUM_TESTS-1][8]={
  170.     {0x92,0x95,0xB5,0x9B,0xB3,0x84,0x73,0x6E},
  171.     {0x19,0x9E,0x9D,0x6D,0xF3,0x9A,0xA8,0x16},
  172.     {0x2A,0x4B,0x4D,0x24,0x52,0x43,0x84,0x27},
  173.     {0x35,0x84,0x3C,0x01,0x9D,0x18,0xC5,0xB6},
  174.     {0x4A,0x5B,0x2F,0x42,0xAA,0x77,0x19,0x25},
  175.     {0xA0,0x6B,0xA9,0xB8,0xCA,0x5B,0x17,0x8A},
  176.     {0xAB,0x9D,0xB7,0xFB,0xED,0x95,0xF2,0x74},
  177.     {0x3D,0x25,0x6C,0x23,0xA7,0x25,0x2F,0xD6},
  178.     {0xB7,0x6F,0xAB,0x4F,0xBD,0xBD,0xB7,0x67},
  179.     {0x8F,0x68,0x27,0xD6,0x9C,0xF4,0x1A,0x10},
  180.     {0x82,0x57,0xA1,0xD6,0x50,0x5E,0x81,0x85},
  181.     {0xA2,0x0F,0x0A,0xCD,0x80,0x89,0x7D,0xFA},
  182.     {0xCD,0x2A,0x53,0x3A,0xDB,0x0D,0x7E,0xF3},
  183.     {0xD2,0xC2,0xBE,0x27,0xE8,0x1B,0x68,0xE3},
  184.     {0xE9,0x24,0xCF,0x4F,0x89,0x3C,0x5B,0x0A},
  185.     {0xA7,0x18,0xC3,0x9F,0xFA,0x9F,0xD7,0x69},
  186.     {0x77,0x2C,0x79,0xB1,0xD2,0x31,0x7E,0xB1},
  187.     {0x49,0xAB,0x92,0x7F,0xD0,0x22,0x00,0xB7},
  188.     {0xCE,0x1C,0x6C,0x7D,0x85,0xE3,0x4A,0x6F},
  189.     {0xBE,0x91,0xD6,0xE1,0x27,0xB2,0xE9,0x87},
  190.     {0x70,0x28,0xAE,0x8F,0xD1,0xF5,0x74,0x1A},
  191.     {0xAA,0x37,0x80,0xBB,0xF3,0x22,0x1D,0xDE},
  192.     {0xA6,0xC4,0xD2,0x5E,0x28,0x93,0xAC,0xB3},
  193.     {0x22,0x07,0x81,0x5A,0xE4,0xB7,0x1A,0xAD},
  194.     {0xDC,0xCE,0x05,0xE7,0x07,0xBD,0xF5,0x84},
  195.     {0x26,0x1D,0x39,0x2C,0xB3,0xBA,0xA5,0x85},
  196.     {0xB4,0xF7,0x0F,0x72,0xFB,0x04,0xF0,0xDC},
  197.     {0x95,0xBA,0xA9,0x4E,0x87,0x36,0xF2,0x89},
  198.     {0xD4,0x07,0x3A,0xF1,0x5A,0x17,0x82,0x0E},
  199.     {0xEF,0x6F,0xAF,0xA7,0x66,0x1A,0x7E,0x89},
  200.     {0xC1,0x97,0xF5,0x58,0x74,0x8A,0x20,0xE7},
  201.     {0x43,0x34,0xCF,0xDA,0x22,0xC4,0x86,0xC8},
  202.     {0x08,0xD7,0xB4,0xFB,0x62,0x9D,0x08,0x85}};
  203.  
  204. static unsigned char cbc_key [8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
  205. static unsigned char cbc2_key[8]={0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87};
  206. static unsigned char cbc3_key[8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
  207. static unsigned char cbc_iv  [8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
  208. static char cbc_data[40]="7654321 Now is the time for ";
  209.  
  210. static unsigned char cbc_ok[32]={
  211.     0xcc,0xd1,0x73,0xff,0xab,0x20,0x39,0xf4,
  212.     0xac,0xd8,0xae,0xfd,0xdf,0xd8,0xa1,0xeb,
  213.     0x46,0x8e,0x91,0x15,0x78,0x88,0xba,0x68,
  214.     0x1d,0x26,0x93,0x97,0xf7,0xfe,0x62,0xb4};
  215.  
  216. static unsigned char cbc3_ok[32]={
  217.     0x3F,0xE3,0x01,0xC9,0x62,0xAC,0x01,0xD0,
  218.     0x22,0x13,0x76,0x3C,0x1C,0xBD,0x4C,0xDC,
  219.     0x79,0x96,0x57,0xC0,0x64,0xEC,0xF5,0xD4,
  220.     0x1C,0x67,0x38,0x12,0xCF,0xDE,0x96,0x75};
  221.  
  222. static unsigned char pcbc_ok[32]={
  223.     0xcc,0xd1,0x73,0xff,0xab,0x20,0x39,0xf4,
  224.     0x6d,0xec,0xb4,0x70,0xa0,0xe5,0x6b,0x15,
  225.     0xae,0xa6,0xbf,0x61,0xed,0x7d,0x9c,0x9f,
  226.     0xf7,0x17,0x46,0x3b,0x8a,0xb3,0xcc,0x88};
  227.  
  228. static unsigned char cfb_key[8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
  229. static unsigned char cfb_iv[8]={0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
  230. static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
  231. static unsigned char plain[24]=
  232.     {
  233.     0x4e,0x6f,0x77,0x20,0x69,0x73,
  234.     0x20,0x74,0x68,0x65,0x20,0x74,
  235.     0x69,0x6d,0x65,0x20,0x66,0x6f,
  236.     0x72,0x20,0x61,0x6c,0x6c,0x20
  237.     };
  238. static unsigned char cfb_cipher8[24]= {
  239.     0xf3,0x1f,0xda,0x07,0x01,0x14, 0x62,0xee,0x18,0x7f,0x43,0xd8,
  240.     0x0a,0x7c,0xd9,0xb5,0xb0,0xd2, 0x90,0xda,0x6e,0x5b,0x9a,0x87 };
  241. static unsigned char cfb_cipher16[24]={
  242.     0xF3,0x09,0x87,0x87,0x7F,0x57, 0xF7,0x3C,0x36,0xB6,0xDB,0x70,
  243.     0xD8,0xD5,0x34,0x19,0xD3,0x86, 0xB2,0x23,0xB7,0xB2,0xAD,0x1B };
  244. static unsigned char cfb_cipher32[24]={
  245.     0xF3,0x09,0x62,0x49,0xA4,0xDF, 0xA4,0x9F,0x33,0xDC,0x7B,0xAD,
  246.     0x4C,0xC8,0x9F,0x64,0xE4,0x53, 0xE5,0xEC,0x67,0x20,0xDA,0xB6 };
  247. static unsigned char cfb_cipher48[24]={
  248.     0xF3,0x09,0x62,0x49,0xC7,0xF4, 0x30,0xB5,0x15,0xEC,0xBB,0x85,
  249.     0x97,0x5A,0x13,0x8C,0x68,0x60, 0xE2,0x38,0x34,0x3C,0xDC,0x1F };
  250. static unsigned char cfb_cipher64[24]={
  251.     0xF3,0x09,0x62,0x49,0xC7,0xF4, 0x6E,0x51,0xA6,0x9E,0x83,0x9B,
  252.     0x1A,0x92,0xF7,0x84,0x03,0x46, 0x71,0x33,0x89,0x8E,0xA6,0x22 };
  253.  
  254. static unsigned char ofb_key[8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
  255. static unsigned char ofb_iv[8]={0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
  256. static unsigned char ofb_buf1[24],ofb_buf2[24],ofb_tmp[8];
  257. static unsigned char ofb_cipher[24]=
  258.     {
  259.     0xf3,0x09,0x62,0x49,0xc7,0xf4,0x6e,0x51,
  260.     0x35,0xf2,0x4a,0x24,0x2e,0xeb,0x3d,0x3f,
  261.     0x3d,0x6d,0x5b,0xe3,0x25,0x5a,0xf8,0xc3
  262.     };
  263.  
  264. unsigned long cbc_cksum_ret=0xB462FEF7L;
  265. unsigned char cbc_cksum_data[8]={0x1D,0x26,0x93,0x97,0xf7,0xfe,0x62,0xb4};
  266.  
  267. #ifdef PROTO
  268. static char *pt(unsigned char *p);
  269. static int cfb_test(int bits, unsigned char *cfb_cipher);
  270. static int cfb64_test(unsigned char *cfb_cipher);
  271. static int ede_cfb64_test(unsigned char *cfb_cipher);
  272. #else
  273. static char *pt();
  274. static int cfb_test();
  275. static int cfb64_test();
  276. static int ede_cfb64_test();
  277. #endif
  278.  
  279. int main(argc,argv)
  280. int argc;
  281. char *argv[];
  282.     {
  283.     int i,j,err=0;
  284.     des_cblock in,out,outin,iv3;
  285.     des_key_schedule ks,ks2,ks3;
  286.     unsigned char cbc_in[40];
  287.     unsigned char cbc_out[40];
  288.     unsigned long cs;
  289.     unsigned char qret[4][4],cret[8];
  290.     unsigned long lqret[4];
  291.     int num;
  292.     char *str;
  293.  
  294.     printf("Doing ecb\n");
  295.     for (i=0; i<NUM_TESTS; i++)
  296.         {
  297.         if ((j=key_sched((C_Block *)(key_data[i]),ks)) != 0)
  298.             {
  299.             printf("Key error %2d:%d\n",i+1,j);
  300.             err=1;
  301.             }
  302.         memcpy(in,plain_data[i],8);
  303.         memset(out,0,8);
  304.         memset(outin,0,8);
  305.         des_ecb_encrypt((C_Block *)in,(C_Block *)out,ks,DES_ENCRYPT);
  306.         des_ecb_encrypt((C_Block *)out,(C_Block *)outin,ks,DES_DECRYPT);
  307.  
  308.         if (memcmp(out,cipher_data[i],8) != 0)
  309.             {
  310.             printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
  311.                 i+1,pt(key_data[i]),pt(in),pt(cipher_data[i]),
  312.                 pt(out));
  313.             err=1;
  314.             }
  315.         if (memcmp(in,outin,8) != 0)
  316.             {
  317.             printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
  318.                 i+1,pt(key_data[i]),pt(out),pt(in),pt(outin));
  319.             err=1;
  320.             }
  321.         }
  322.  
  323.     printf("Doing ede ecb\n");
  324.     for (i=0; i<(NUM_TESTS-1); i++)
  325.         {
  326.         if ((j=key_sched((C_Block *)(key_data[i]),ks)) != 0)
  327.             {
  328.             err=1;
  329.             printf("Key error %2d:%d\n",i+1,j);
  330.             }
  331.         if ((j=key_sched((C_Block *)(key_data[i+1]),ks2)) != 0)
  332.             {
  333.             printf("Key error %2d:%d\n",i+2,j);
  334.             err=1;
  335.             }
  336.         if ((j=key_sched((C_Block *)(key_data[i+2]),ks3)) != 0)
  337.             {
  338.             printf("Key error %2d:%d\n",i+3,j);
  339.             err=1;
  340.             }
  341.         memcpy(in,plain_data[i],8);
  342.         memset(out,0,8);
  343.         memset(outin,0,8);
  344.         des_ecb2_encrypt((C_Block *)in,(C_Block *)out,ks,ks2,
  345.             DES_ENCRYPT);
  346.         des_ecb2_encrypt((C_Block *)out,(C_Block *)outin,ks,ks2,
  347.             DES_DECRYPT);
  348.  
  349.         if (memcmp(out,cipher_ecb2[i],8) != 0)
  350.             {
  351.             printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
  352.                 i+1,pt(key_data[i]),pt(in),pt(cipher_ecb2[i]),
  353.                 pt(out));
  354.             err=1;
  355.             }
  356.         if (memcmp(in,outin,8) != 0)
  357.             {
  358.             printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
  359.                 i+1,pt(key_data[i]),pt(out),pt(in),pt(outin));
  360.             err=1;
  361.             }
  362.         }
  363.  
  364.     printf("Doing cbc\n");
  365.     if ((j=key_sched((C_Block *)cbc_key,ks)) != 0)
  366.         {
  367.         printf("Key error %d\n",j);
  368.         err=1;
  369.         }
  370.     memset(cbc_out,0,40);
  371.     memset(cbc_in,0,40);
  372.     des_cbc_encrypt((C_Block *)cbc_data,(C_Block *)cbc_out,
  373.         (long)strlen((char *)cbc_data)+1,ks,
  374.         (C_Block *)cbc_iv,DES_ENCRYPT);
  375.     if (memcmp(cbc_out,cbc_ok,32) != 0)
  376.         printf("cbc_encrypt encrypt error\n");
  377.     des_cbc_encrypt((C_Block *)cbc_out,(C_Block *)cbc_in,
  378.         (long)strlen((char *)cbc_data)+1,ks,
  379.         (C_Block *)cbc_iv,DES_DECRYPT);
  380.     if (memcmp(cbc_in,cbc_data,32) != 0)
  381.         {
  382.         printf("cbc_encrypt decrypt error\n");
  383.         err=1;
  384.         }
  385.  
  386.     printf("Doing ede cbc\n");
  387.     if ((j=key_sched((C_Block *)cbc_key,ks)) != 0)
  388.         {
  389.         printf("Key error %d\n",j);
  390.         err=1;
  391.         }
  392.     if ((j=key_sched((C_Block *)cbc2_key,ks2)) != 0)
  393.         {
  394.         printf("Key error %d\n",j);
  395.         err=1;
  396.         }
  397.     if ((j=key_sched((C_Block *)cbc3_key,ks3)) != 0)
  398.         {
  399.         printf("Key error %d\n",j);
  400.         err=1;
  401.         }
  402.     memset(cbc_out,0,40);
  403.     memset(cbc_in,0,40);
  404.     i=strlen((char *)cbc_data)+1;
  405.     i=((i+7)/8)*8;
  406.     memcpy(iv3,cbc_iv,sizeof(cbc_iv));
  407.  
  408.     des_ede3_cbc_encrypt((C_Block *)cbc_data,(C_Block *)cbc_out,
  409.         16L,ks,ks2,ks3,(C_Block *)iv3,DES_ENCRYPT);
  410.     des_ede3_cbc_encrypt((C_Block *)&(cbc_data[16]),
  411.         (C_Block *)&(cbc_out[16]),
  412.         (long)i-16,ks,ks2,ks3,(C_Block *)iv3,DES_ENCRYPT);
  413.     if (memcmp(cbc_out,cbc3_ok,
  414.         (unsigned int)(strlen((char *)cbc_data)+1+7)/8*8) != 0)
  415.         {
  416.         printf("des_ede3_cbc_encrypt encrypt error\n");
  417.         err=1;
  418.         }
  419.  
  420.     memcpy(iv3,cbc_iv,sizeof(cbc_iv));
  421.     des_ede3_cbc_encrypt((C_Block *)cbc_out,(C_Block *)cbc_in,
  422.         (long)i,ks,ks2,ks3,(C_Block *)iv3,DES_DECRYPT);
  423.     if (memcmp(cbc_in,cbc_data,strlen(cbc_data)+1) != 0)
  424.         {
  425.         printf("des_ede3_cbc_encrypt decrypt error\n");
  426.         err=1;
  427.         }
  428.  
  429.     printf("Doing pcbc\n");
  430.     if ((j=key_sched((C_Block *)cbc_key,ks)) != 0)
  431.         {
  432.         printf("Key error %d\n",j);
  433.         err=1;
  434.         }
  435.     memset(cbc_out,0,40);
  436.     memset(cbc_in,0,40);
  437.     des_pcbc_encrypt((C_Block *)cbc_data,(C_Block *)cbc_out,
  438.         (long)strlen(cbc_data)+1,ks,(C_Block *)cbc_iv,DES_ENCRYPT);
  439.     if (memcmp(cbc_out,pcbc_ok,32) != 0)
  440.         {
  441.         printf("pcbc_encrypt encrypt error\n");
  442.         err=1;
  443.         }
  444.     des_pcbc_encrypt((C_Block *)cbc_out,(C_Block *)cbc_in,
  445.         (long)strlen(cbc_data)+1,ks,(C_Block *)cbc_iv,DES_DECRYPT);
  446.     if (memcmp(cbc_in,cbc_data,32) != 0)
  447.         {
  448.         printf("pcbc_encrypt decrypt error\n");
  449.         err=1;
  450.         }
  451.  
  452.     printf("Doing ");
  453.     printf("cfb8 ");
  454.     err+=cfb_test(8,cfb_cipher8);
  455.     printf("cfb16 ");
  456.     err+=cfb_test(16,cfb_cipher16);
  457.     printf("cfb32 ");
  458.     err+=cfb_test(32,cfb_cipher32);
  459.     printf("cfb48 ");
  460.     err+=cfb_test(48,cfb_cipher48);
  461.     printf("cfb64 ");
  462.     err+=cfb_test(64,cfb_cipher64);
  463.  
  464.     printf("cfb64() ");
  465.     err+=cfb64_test(cfb_cipher64);
  466.  
  467.     memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
  468.     for (i=0; i<sizeof(plain); i++)
  469.         des_cfb_encrypt(&(plain[i]),&(cfb_buf1[i]),
  470.             8,(long)1,ks,(C_Block *)cfb_tmp,DES_ENCRYPT);
  471.     if (memcmp(cfb_cipher8,cfb_buf1,sizeof(plain)) != 0)
  472.         {
  473.         printf("cfb_encrypt small encrypt error\n");
  474.         err=1;
  475.         }
  476.  
  477.     memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
  478.     for (i=0; i<sizeof(plain); i++)
  479.         des_cfb_encrypt(&(cfb_buf1[i]),&(cfb_buf2[i]),
  480.             8,(long)1,ks,(C_Block *)cfb_tmp,DES_DECRYPT);
  481.     if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
  482.         {
  483.         printf("cfb_encrypt small decrypt error\n");
  484.         err=1;
  485.         }
  486.  
  487.     printf("ede_cfb64() ");
  488.     err+=ede_cfb64_test(cfb_cipher64);
  489.  
  490.     printf("done\n");
  491.  
  492.     printf("Doing ofb\n");
  493.     key_sched((C_Block *)ofb_key,ks);
  494.     memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
  495.     des_ofb_encrypt(plain,ofb_buf1,64,(long)sizeof(plain)/8,ks,
  496.         (C_Block *)ofb_tmp);
  497.     if (memcmp(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
  498.         {
  499.         printf("ofb_encrypt encrypt error\n");
  500.         err=1;
  501.         }
  502.     memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
  503.     des_ofb_encrypt(ofb_buf1,ofb_buf2,64,(long)sizeof(ofb_buf1)/8,ks,
  504.         (C_Block *)ofb_tmp);
  505.     if (memcmp(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
  506.         {
  507.         printf("ofb_encrypt decrypt error\n");
  508.         err=1;
  509.         }
  510.  
  511.     printf("Doing ofb64\n");
  512.     key_sched((C_Block *)ofb_key,ks);
  513.     memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
  514.     memset(ofb_buf1,0,sizeof(ofb_buf1));
  515.     memset(ofb_buf2,0,sizeof(ofb_buf1));
  516.     num=0;
  517.     for (i=0; i<sizeof(plain); i++)
  518.         {
  519.         des_ofb64_encrypt(&(plain[i]),&(ofb_buf1[i]),1,ks,
  520.             (C_Block *)ofb_tmp,&num);
  521.         }
  522.     if (memcmp(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
  523.         {
  524.         printf("ofb64_encrypt encrypt error\n");
  525.         err=1;
  526.         }
  527.     memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
  528.     num=0;
  529.     des_ofb64_encrypt(ofb_buf1,ofb_buf2,(long)sizeof(ofb_buf1),ks,
  530.         (C_Block *)ofb_tmp,&num);
  531.     if (memcmp(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
  532.         {
  533.         printf("ofb64_encrypt decrypt error\n");
  534.         err=1;
  535.         }
  536.  
  537.     printf("Doing ede_ofb64\n");
  538.     key_sched((C_Block *)ofb_key,ks);
  539.     memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
  540.     memset(ofb_buf1,0,sizeof(ofb_buf1));
  541.     memset(ofb_buf2,0,sizeof(ofb_buf1));
  542.     num=0;
  543.     for (i=0; i<sizeof(plain); i++)
  544.         {
  545.         des_ede3_ofb64_encrypt(&(plain[i]),&(ofb_buf1[i]),1,ks,ks,ks,
  546.             (C_Block *)ofb_tmp,&num);
  547.         }
  548.     if (memcmp(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
  549.         {
  550.         printf("ede_ofb64_encrypt encrypt error\n");
  551.         err=1;
  552.         }
  553.     memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
  554.     num=0;
  555.     des_ede3_ofb64_encrypt(ofb_buf1,ofb_buf2,(long)sizeof(ofb_buf1),ks,
  556.         ks,ks,(C_Block *)ofb_tmp,&num);
  557.     if (memcmp(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
  558.         {
  559.         printf("ede_ofb64_encrypt decrypt error\n");
  560.         err=1;
  561.         }
  562.  
  563.     printf("Doing cbc_cksum\n");
  564.     key_sched((C_Block *)cbc_key,ks);
  565.     cs=cbc_cksum((C_Block *)cbc_data,(C_Block *)cret,
  566.         (long)strlen(cbc_data),ks,(C_Block *)cbc_iv);
  567.     if (cs != cbc_cksum_ret)
  568.         {
  569.         printf("bad return value (%08lX), should be %08lX\n",
  570.             cs,cbc_cksum_ret);
  571.         err=1;
  572.         }
  573.     if (memcmp(cret,cbc_cksum_data,8) != 0)
  574.         {
  575.         printf("bad cbc_cksum block returned\n");
  576.         err=1;
  577.         }
  578.  
  579.     printf("Doing quad_cksum\n");
  580.     cs=quad_cksum((C_Block *)cbc_data,(C_Block *)qret,
  581.         (long)strlen(cbc_data),2,(C_Block *)cbc_iv);
  582.     for (i=0; i<4; i++)
  583.         {
  584.         lqret[i]=0;
  585.         memcpy(&(lqret[i]),&(qret[i][0]),4);
  586.         }
  587.     { /* Big-endian fix */
  588.     static unsigned long l=1;
  589.     static unsigned char *c=(unsigned char *)&l;
  590.     unsigned long ll;
  591.  
  592.     if (!c[0])
  593.         {
  594.         ll=lqret[0]^lqret[3];
  595.         lqret[0]^=ll;
  596.         lqret[3]^=ll;
  597.         ll=lqret[1]^lqret[2];
  598.         lqret[1]^=ll;
  599.         lqret[2]^=ll;
  600.         }
  601.     }
  602.     if (cs != 0x70d7a63aL)
  603.         {
  604.         printf("quad_cksum error, ret %08lx should be 70d7a63a\n",cs);
  605.         err=1;
  606.         }
  607.     if (lqret[0] != 0x327eba8dL)
  608.         {
  609.         printf("quad_cksum error, out[0] %08lx is not %08lx\n",
  610.             lqret[0],0x327eba8dL);
  611.         err=1;
  612.         }
  613.     if (lqret[1] != 0x201a49ccL)
  614.         {
  615.         printf("quad_cksum error, out[1] %08lx is not %08lx\n",
  616.             lqret[1],0x201a49ccL);
  617.         err=1;
  618.         }
  619.     if (lqret[2] != 0x70d7a63aL)
  620.         {
  621.         printf("quad_cksum error, out[2] %08lx is not %08lx\n",
  622.             lqret[2],0x70d7a63aL);
  623.         err=1;
  624.         }
  625.     if (lqret[3] != 0x501c2c26L)
  626.         {
  627.         printf("quad_cksum error, out[3] %08lx is not %08lx\n",
  628.             lqret[3],0x501c2c26L);
  629.         err=1;
  630.         }
  631.  
  632.     printf("input word alignment test");
  633.     for (i=0; i<4; i++)
  634.         {
  635.         printf(" %d",i);
  636.         des_cbc_encrypt((C_Block *)&(cbc_out[i]),(C_Block *)cbc_in,
  637.             (long)strlen(cbc_data)+1,ks,(C_Block *)cbc_iv,
  638.             DES_ENCRYPT);
  639.         }
  640.     printf("\noutput word alignment test");
  641.     for (i=0; i<4; i++)
  642.         {
  643.         printf(" %d",i);
  644.         des_cbc_encrypt((C_Block *)cbc_out,(C_Block *)&(cbc_in[i]),
  645.             (long)strlen(cbc_data)+1,ks,(C_Block *)cbc_iv,
  646.             DES_ENCRYPT);
  647.         }
  648.     printf("\n");
  649.     printf("fast crypt test ");
  650.     str=crypt("testing","ef");
  651.     if (strcmp("efGnQx2725bI2",str) != 0)
  652.         {
  653.         printf("fast crypt error, %s should be efGnQx2725bI2\n",str);
  654.         err=1;
  655.         }
  656.     str=crypt("bca76;23","yA");
  657.     if (strcmp("yA1Rp/1hZXIJk",str) != 0)
  658.         {
  659.         printf("fast crypt error, %s should be yA1Rp/1hZXIJk\n",str);
  660.         err=1;
  661.         }
  662.     printf("\n");
  663.     exit(err);
  664. #ifdef LINT
  665.     return(0);
  666. #endif
  667.     }
  668.  
  669. static char *pt(p)
  670. unsigned char *p;
  671.     {
  672.     static char bufs[10][20];
  673.     static int bnum=0;
  674.     char *ret;
  675.     int i;
  676.     static char *f="0123456789ABCDEF";
  677.  
  678.     ret= &(bufs[bnum++][0]);
  679.     bnum%=10;
  680.     for (i=0; i<8; i++)
  681.         {
  682.         ret[i*2]=f[(p[i]>>4)&0xf];
  683.         ret[i*2+1]=f[p[i]&0xf];
  684.         }
  685.     ret[16]='\0';
  686.     return(ret);
  687.     }
  688.  
  689. static int cfb_test(bits, cfb_cipher)
  690. int bits;
  691. unsigned char *cfb_cipher;
  692.     {
  693.     des_key_schedule ks;
  694.     int i,err=0;
  695.  
  696.     key_sched((C_Block *)cfb_key,ks);
  697.     memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
  698.     des_cfb_encrypt(plain,cfb_buf1,bits,(long)sizeof(plain),ks,
  699.         (C_Block *)cfb_tmp,DES_ENCRYPT);
  700.     if (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
  701.         {
  702.         err=1;
  703.         printf("cfb_encrypt encrypt error\n");
  704.         for (i=0; i<24; i+=8)
  705.             printf("%s\n",pt(&(cfb_buf1[i])));
  706.         }
  707.     memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
  708.     des_cfb_encrypt(cfb_buf1,cfb_buf2,bits,(long)sizeof(plain),ks,
  709.         (C_Block *)cfb_tmp,DES_DECRYPT);
  710.     if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
  711.         {
  712.         err=1;
  713.         printf("cfb_encrypt decrypt error\n");
  714.         for (i=0; i<24; i+=8)
  715.             printf("%s\n",pt(&(cfb_buf1[i])));
  716.         }
  717.     return(err);
  718.     }
  719.  
  720. static int cfb64_test(cfb_cipher)
  721. unsigned char *cfb_cipher;
  722.     {
  723.     des_key_schedule ks;
  724.     int err=0,i,n;
  725.  
  726.     key_sched((C_Block *)cfb_key,ks);
  727.     memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
  728.     n=0;
  729.     des_cfb64_encrypt(plain,cfb_buf1,(long)12,ks,
  730.         (C_Block *)cfb_tmp,&n,DES_ENCRYPT);
  731.     des_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
  732.         (long)sizeof(plain)-12,ks,
  733.         (C_Block *)cfb_tmp,&n,DES_ENCRYPT);
  734.     if (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
  735.         {
  736.         err=1;
  737.         printf("cfb_encrypt encrypt error\n");
  738.         for (i=0; i<24; i+=8)
  739.             printf("%s\n",pt(&(cfb_buf1[i])));
  740.         }
  741.     memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
  742.     n=0;
  743.     des_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,ks,
  744.         (C_Block *)cfb_tmp,&n,DES_DECRYPT);
  745.     des_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
  746.         (long)sizeof(plain)-17,ks,
  747.         (C_Block *)cfb_tmp,&n,DES_DECRYPT);
  748.     if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
  749.         {
  750.         err=1;
  751.         printf("cfb_encrypt decrypt error\n");
  752.         for (i=0; i<24; i+=8)
  753.             printf("%s\n",pt(&(cfb_buf2[i])));
  754.         }
  755.     return(err);
  756.     }
  757.  
  758. static int ede_cfb64_test(cfb_cipher)
  759. unsigned char *cfb_cipher;
  760.     {
  761.     des_key_schedule ks;
  762.     int err=0,i,n;
  763.  
  764.     key_sched((C_Block *)cfb_key,ks);
  765.     memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
  766.     n=0;
  767.     des_ede3_cfb64_encrypt(plain,cfb_buf1,(long)12,ks,ks,ks,
  768.         (C_Block *)cfb_tmp,&n,DES_ENCRYPT);
  769.     des_ede3_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
  770.         (long)sizeof(plain)-12,ks,ks,ks,
  771.         (C_Block *)cfb_tmp,&n,DES_ENCRYPT);
  772.     if (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
  773.         {
  774.         err=1;
  775.         printf("ede_cfb_encrypt encrypt error\n");
  776.         for (i=0; i<24; i+=8)
  777.             printf("%s\n",pt(&(cfb_buf1[i])));
  778.         }
  779.     memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
  780.     n=0;
  781.     des_ede3_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,ks,ks,ks,
  782.         (C_Block *)cfb_tmp,&n,DES_DECRYPT);
  783.     des_ede3_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
  784.         (long)sizeof(plain)-17,ks,ks,ks,
  785.         (C_Block *)cfb_tmp,&n,DES_DECRYPT);
  786.     if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
  787.         {
  788.         err=1;
  789.         printf("ede_cfb_encrypt decrypt error\n");
  790.         for (i=0; i<24; i+=8)
  791.             printf("%s\n",pt(&(cfb_buf2[i])));
  792.         }
  793.     return(err);
  794.     }
  795.